<?php


namespace App\server\SMS;

use App\server\SMS\SMSServiceFactory;
use Illuminate\Support\Facades\Cache;
class CodeServer {
    //发送验证码

    public static function sendCode($tel,$type){
        // 使用工厂来发送短信
        $SMS = SMSServiceFactory::getService($type);
        $code=$SMS->send($tel);
        Cache::set('phone',$tel,60);
        return $code;
    }
    //验证验证码
    public static function verifyCode($key){

        $phone= Cache::get('phone');
        $code=Cache::get('code');
        if(empty($phone))throw new \Exception('验证码已失效，请重新发送');
        if($key['phone']!=$phone)throw new \Exception('手机号不正确');
        if($key['code']!=$code)throw new \Exception('验证码不正确');
        return true;
    }
}
